package org.springblade.modules.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.sms.model.SmsResponse;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.NotePushDTO;
import org.springblade.modules.admin.dto.TlwBuildingElevatorQuery;
import org.springblade.modules.admin.dto.tlw.TaskStatusDTO;
import org.springblade.modules.admin.dto.tlw.TlwPlanQueryDTO;
import org.springblade.modules.admin.service.IMaintainPlanConfigService;
import org.springblade.modules.admin.service.MaintainSignSetingService;
import org.springblade.modules.admin.service.TlwService;
import org.springblade.modules.admin.dto.TlwAlarmNoticeDTO;
import org.springblade.modules.admin.vo.*;
import org.springblade.modules.admin.vo.tlw.TlwByPlanVO;
import org.springblade.modules.admin.vo.tlw.TlwElevatorStatistics;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author Jackey
 * @date 2022年10月13日 14:17
 */
@Slf4j
@RestController
@RequestMapping("/admin/tlw")
@Api(value = "供梯联网接口", tags = "供梯联网接口")
public class TlwController {

	@Autowired
	private TlwService tlwService;
	@Autowired
	private IMaintainPlanConfigService maintainPlanConfigService;
	@Autowired
	private MaintainSignSetingService signSetingService;
	@Autowired
	private ITenantService tenantService;

	/**
	 * 梯联网统计
	 *
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/statistics")
	public R<TlwStatisticsVO> statistics() {

		TlwStatisticsVO tlwStatisticsVO = tlwService.statistics();
		return R.data(tlwStatisticsVO);
	}

	/**
	 * 梯联网排行榜
	 */
	@TenantIgnore
	@GetMapping("/rankinglist")
	public R<List<TlwRankingListVO>> rankingList(String deviceListString) {
		if (StrUtil.isEmpty(deviceListString)) {
			throw new ServiceException("数据不能为空");
		}
		List<String> deviceList = StrUtil.split(deviceListString, ',');

		return R.data(tlwService.rankingList(deviceList));
	}

	/**
	 * 物业人员列表
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/estatePeopleList")
	public R<List<TlwEstatePeopleVO>> estatePeopleList() {

		return R.data(tlwService.estatePeopleList());
	}

	/**
	 * 推送通知: 维保,物业单独服务处理,此处仅处理业务逻辑
	 * @param noticeDTO
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/alarmNotice")
	public R<Boolean> alarmNotice(@RequestBody TlwAlarmNoticeDTO noticeDTO) {
		return R.status(tlwService.alarmNotice(noticeDTO));
	}

	/**
	 * 维保人员列表
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/maintainPeopleList")
	public R<List<TlwEstatePeopleVO>> maintainPeopleList(String deviceId) {
		return R.data(tlwService.maintainPeopleList(deviceId));
	}

	/**
	 * 维保人员列表
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/batch/maintainPeopleList")
	public R<List<Map<String,Object>>> batchMaintainPeopleList(String deviceIds) {
		return R.data(tlwService.batchMaintainPeopleList(deviceIds));
	}


	/**
	 * 项目下电梯（已关联设备id）
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/building/device/ele/list")
	public R<List<BuildingDeviceEleVO>> buildingDeviceEleList(@RequestParam(required = false) Long buildingId) {
		return R.data(tlwService.buildingDeviceEleList(buildingId));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   梯联网信息同步 获取项目电梯物业管理人列表
	 * }
	 * @Date 14:42 2023/2/22
	 * @Param
	 * @return
	 **/
	@TenantIgnore
	@GetMapping("/building/elevator/page")
	public R<Map<String,Object>> buildingElevatorPage(TlwBuildingElevatorQuery tbeQuery, Query query){
		return R.data(tlwService.buildingElevatorPage(tbeQuery,query));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   根据 deviceId 获取对应项目电梯信息
	 * }
	 * @Date 14:42 2023/2/22
	 * @Param
	 * @return
	 **/
	@TenantIgnore
	@GetMapping("/building/elevator/device/page")
	public R<Map<String,Object>> deviceBuildingElevatorPage(TlwBuildingElevatorQuery tbeQuery, Query query){
		return R.data(tlwService.deviceBuildingElevatorPage(tbeQuery,query));
	}

	/**
	 * 维保人员列表
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/batch/elevator/maintainPeopleList")
	public R<List<Map<String,Object>>> batchElevatorMaintainPeopleList(String elevatorIds) {
		return R.data(tlwService.batchElevatorMaintainPeopleList(elevatorIds));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   梯联网发送短信
	 * }
	 * @Date 14:42 2023/6/16
	 * @Param
	 * @return
	 **/
	@PostMapping("/note/push")
	public R<Boolean> nodePush(@RequestBody List<NotePushDTO> pushDTOList){
		if(ObjectUtil.isNotEmpty(pushDTOList)){
			pushDTOList.forEach(e -> {
				log.info("===========发送推送短信,手机号码:[{}]===========", e.getPhone());
				SmsResponse smsResponse = SmsUtil.sendMessage(e.getSmsCode(), BladeConstant.ADMIN_TENANT_ID,e.getParams(),e.getPhone());
				log.info("===========发送推送短信,发送结果:[{}]===========", smsResponse.toString());
			});
		}
		return R.status(Boolean.TRUE);
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   梯联网查询保养工单分页
	 * }
	 * @Date 15:02 2023/6/17
	 * @Param
	 * @return
	 **/
	@TenantIgnore
	@PostMapping("/by/plan/page")
	public R<IPage<TlwByPlanVO>> tlwByPlanPage(@RequestBody TlwPlanQueryDTO queryDTO,@RequestBody Query query){
		return R.data(tlwService.tlwByPlanPage(queryDTO, Condition.getPage(query)));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   梯联网查询保养工单列表
	 * }
	 * @Date 15:02 2023/6/17
	 * @Param
	 * @return
	 **/
	@TenantIgnore
	@PostMapping("/by/plan/list")
	public R<List<TlwByPlanVO>> tlwByPlanList(@RequestBody TlwPlanQueryDTO queryDTO){
		return R.data(tlwService.tlwByPlanList(queryDTO));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   梯联网查询保养工单详情
	 * }
	 * @Date 10:36 2023/6/21
	 * @Param
	 * @return
	 **/
	@TenantIgnore
	@GetMapping("/by/plan/detail")
	public R<TlwByPlanVO> tlwByPlanDetail(Long planId){
		return R.data(tlwService.tlwByPlanDetail(planId));
	}

	/**
	 * 获取任务工作项
	 * @param planId
	 * @param templateId
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/by/plan/work/item")
	public R<List<PlanTemplateVO>> workItem(Long planId, Long templateId,Integer isStore){
		return R.data(tlwService.getPlanWorkItem(planId,templateId,isStore));
	}

	/**
	 * 自定义分页 保养计划配置
	 */
	@TenantIgnore
	@PostMapping("/plan/config/page")
	public R<IPage<MaintainPlanConfigVO>> page(@RequestBody MaintainPlanConfigVO maintainPlanConfig,@RequestBody Query query) {
		IPage<MaintainPlanConfigVO> pages = maintainPlanConfigService.selectMaintainPlanConfigPage(Condition.getPage(query), maintainPlanConfig);
		return R.data(pages);
	}

	@GetMapping("/getHolidays")
	@ApiOperation(value = "获取年度所有节假日")
	public R getHolidays(@ApiParam(value = "获取年度所有节假日，默认本年") @RequestParam(required = false) Integer year) {
		return R.data(signSetingService.getHolidays(year));
	}

	@TenantIgnore
	@PostMapping("/elevator/task/status")
	@ApiOperation(value = "电梯工单任务状态")
	public R<List<ElevatorTaskStatusVO>> getElevatorTaskStatusList(@Valid @RequestBody TaskStatusDTO taskStatusDTO) {
		return R.data(tlwService.getElevatorTaskStatusList(taskStatusDTO));
	}

	@TenantIgnore
	@GetMapping("/getElevatorStatistics")
	@ApiOperation(value = "单个电梯统计数据")
	public R<TlwElevatorStatistics> getElevatorStatistics(@ApiParam(value = "设备id", required = true) @RequestParam String deviceId,
														  @ApiParam(value = "开始时间", required = true) @RequestParam Long startAt,
														  @ApiParam(value = "结束时间", required = true) @RequestParam Long endAt) {
		return R.data(tlwService.getElevatorStatistics(deviceId, startAt, endAt));
	}

	@TenantIgnore
	@GetMapping("/tenant/list")
	public R<List<Tenant>> tenantList(){
		return R.data(tenantService.listAllTenant());
	}

}
